const app = getApp()

import Dialog from '@vant/weapp/dialog/dialog';
import {
    showMsg
} from '../../../../utils/config.js'

import {
    reqAlarmHandlingRecord,
    reqAlarmCloseLoop,
    reqBuildingById,
    reqAlarmPlay
} from '../../../../request/api.js'

Page({

    data: {
        navHeight: app.globalData.navHeight + 'px', //头部距离
        bottomHeight: app.globalData.bottomHeight + 'px', //iphone底部距离
        isTip: true, //是否展示提示
        isBtns: true, //是否展示告警处理按钮
        alarmInfo: {}, //告警信息
        dataList: [],
        triggered: false, //是否下拉刷新列表
        menci: '/statics/images/gaojing/menci.png',
        shexiangji: '/statics/images/gaojing/shexiangji.png',
        honwai: '/statics/images/gaojing/honwai.png',
        yangan: '/statics/images/gaojing/yangan.png',
        ranqi: '/statics/images/gaojing/ranqi.png',
        alarmPlayFlag:false,
        alarmPlayList:[],
        isProprietor:true,//装维||业主
    },

    onLoad(e) {
        this.setData({
            info: e.info,
            alarmType: JSON.parse(e.info).alarmType
        })
        let info = JSON.parse(e.info)
        this.getAlarmInfo(info)
        this.onGetBuildingInfo(info.spaceId)
        this.onAlarmPlay(info.id)
        if((info.status==1|| info.status==2)&& app.globalData.isBtnsAlarm){
            this.setData({
                isBtnsAlarm:true
            })
        }else{
            this.setData({
                isBtnsAlarm:false
            })
        }
    },
    async onShow() {
        if(!app.globalData.isBtnsAlarm){
            this.setData({
                isBtnsAlarm:false,
                'alarmInfo.status': 3
            })
            app.globalData.isBtnsAlarm=true;
        }
        this.setData({
            isProprietor:app.globalData.isProprietor
        })
        app.globalData.isAlarm = true 
        wx.setStorageSync('isAlarm', true);//是否从告警处理记录出来
        await this.getDataList()
        // console.log(this.data.dataList.length>0);
        // console.log(this.data.alarmInfo.status);
        // if (this.data.dataList.length > 0 && this.data.alarmInfo.status == 1) {
        //     this.setData({
        //         'alarmInfo.status': 2
        //     })
        // }
    },
    // 获取告警信息
    getAlarmInfo(info) {
        this.setData({
            alarmInfo: info
        })
        if (info.status === 3) {
            this.setData({
                isBtns: false
            })
        }
    },
    // 获取告警处理记录列表
    async getDataList() {
        const res = await reqAlarmHandlingRecord(this.data.alarmInfo.id)
        if (res.code !== 20000) return showMsg(res.message)
        let {
            row
        } = res.data
        this.setData({
            dataList: row
        })
    },
    // 下拉刷新
    async onRefresh() {
        this.setData({
            dataList: [],
            triggered: true,
        })
        await this.getDataList()
        this.setData({
            triggered: false,
        })
    },

    // 预览图片
    imageClick(e) {
        let {
            item
        } = e.currentTarget.dataset
        if (!item || item == 'null') return
        let {
            dataList
        } = this.data
        let imageList = dataList.map(item => {
            if (item.pic && item.pic != 'null') {
                console.log(item.pic);
                return item.pic
            }
        })
        let arr = imageList.filter(Boolean)
        wx.previewImage({
            current: item, // 当前显示图片的http链接
            urls: arr
        })
    },

    // 点击关闭提示
    clickDeleteTip() {
        this.setData({
            isTip: false
        })
    },
    // 闭环处理
    clickClosedLoop() {
        Dialog.confirm({
                title: '温馨提示',
                message: `是否确定完结？完结后不能再次处理`,
            })
            .then(async () => {
                let {
                    id,
                    imei,
                    orgName,
                    name,
                    classMark,
                    deviceType,
                    spaceId,
                    alarmType,
                    createTime,
                } = this.data.alarmInfo
                let params = {
                    id,
                    imei,
                    orgName,
                    name,
                    classMark,
                    deviceType,
                    spaceId,
                    alarmType,
                    createTime,
                    orgId: app.globalData.orgId ==1?'':app.globalData.orgId,
                    userId: app.globalData.userId
                }
                const res = await reqAlarmCloseLoop(params)
                if (res.code != 20000) return showMsg(res.message)
                showMsg("处理成功", "success")
                this.setData({
                    isBtns: false,
                    'alarmInfo.status': 3
                })
            })
            .catch(() => {});
    },
    // 去告警处理
    goAlarmHandle() {
        let {
            dataList
        } = this.data
        if (dataList.length >= 10) return showMsg("一条告警最多处理10次")
        let info = JSON.stringify(this.data.alarmInfo)
        wx.navigateTo({
            url: `/pages/message-index-page/alarm-handle/alarm-handle?info=${(info)}`,
        })
    },
    // 获取安装点信息
    async onGetBuildingInfo(id) {
        let res = await reqBuildingById(id)
        if (res.code != 20000) return showMsg(res.message)
        this.setData({
            spaceInfo: res.data.rows
        })
        console.log(res, "Rse");
    },
    // 导航
    onNavigation() {
        wx.openLocation({
            latitude: Number(this.data.spaceInfo.lat),
            longitude: Number(this.data.spaceInfo.lng),
            scale: 23,
            name: this.data.spaceInfo.name,
            // address: this.data.navigation.address
        })
    },
    onDetail() {
        // wx.navigateTo({
        //     url: `/packageA/pages/index-inside-page/device-detail/device-detail?info=${this.data.info}`,
        // })
        wx.navigateTo({
            url: `/packageA/pages/index-inside-page/device-detailed/device-detailed?info=${this.data.info}`,
        })
    },
        // 告警id查询告警视频列表
        async onAlarmPlay(id) {
            let res = await reqAlarmPlay(id)
            if (res.code != 20000) return showMsg(res.message)
            console.log(res, "Rse");
            if(res.data.row.length==0){
                 this.setData({
                        alarmPlayFlag: false
                })
                return;
            }
           this.setData({
            alarmPlayList:res.data.row,
            alarmPlayFlag: true
           })
        },
    onPlayback(){
        this.setData({
            show:true,
        })
    },
  
//   窗口关闭时，停止所有视频
    onClose(){
          this.setData({
            videoCtx:wx.createVideoContext('myVideo', this),
            show:false,
          })
      //(暂停视频)
      this.data.videoCtx.stop() 
    }
})